package com.next.muscu.data;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

public class GenericListModel<T extends MyDataObject> extends AbstractListModel implements ListModel, Observer, ComboBoxModel{

	ObservableList<T> data; 
	int comboSelectedItem = -1;
	
	public GenericListModel(ObservableList<T> data) {
		this.data = data;
		data.addObserver(this);
	}

	public List<T> getData() {
		return data;
	}

	public void setData(ObservableList<T> data) {
		this.data.remove(this);
		this.data = data;
		this.data.addObserver(this);
	}

	public Object getElementAt(int arg0) {
		return data.get(arg0);
	}

	public int getSize() {
		return data.size();
	}

	public void update(Observable arg0, Object arg1) {
		//TODO tenir compte de l'intervale de changement pour ne pas rafraichir toute la liste 
		this.fireContentsChanged(this, 0, data.size());
		
	}

	@Override
	public Object getSelectedItem() {
		if(comboSelectedItem >= 0)
			return data.get(comboSelectedItem);
		return null;
	}

	@Override
	public void setSelectedItem(Object arg0) {
		T e = (T) arg0;
		comboSelectedItem = data.indexOf(e);
		
	}
	
	/*public void removeElement(T e)
	{
		int i = data.indexOf(e);
		if(i!=-1)
		{
			data.remove(e);
			this.fireContentsChanged(this, i, i)
		}
	}
	
	public void removeElementByIndex(int i)
	{
		data.remove(i);
		this.fireContentsChanged(this, i, i)
	}
	public void addElement(T e)
	{
		data.add(e);
		this.fireContentsChanged(this, i, i)
	}
	public void addElement(int index,T e)
	{
		data.add(index, e);
		this.fireContentsChanged(this, index, index)
		
	}*/
}
